package com.java.zhuiyun.api.jielongidiomquestion.service.impl;

import com.java.zhuiyun.api.jielongidiomquestion.entity.JieLongIdiomQuestion;
import com.java.zhuiyun.api.jielongidiomquestion.mapper.JieLongIdiomQuestionMapper;
import com.java.zhuiyun.api.jielongidiomquestion.model.JieLongIdiomQuestionModel;
import com.java.zhuiyun.api.jielongidiomquestion.service.JieLongIdiomQuestionService;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName JieLongIdiomQuestionServiceImpl
 * @Description TODO 成语接龙题库业务逻辑层实现类 后台管理查询词库、单查词语信息、保存成语信息、添加成语
 * @Author 杨友朋
 * @Date 2023/9/13 9:19
 **/
@Service
public class JieLongIdiomQuestionServiceImpl implements JieLongIdiomQuestionService {
    @Resource
    JieLongIdiomQuestionMapper jieLongIdiomQuestionMapper;

    /**
     * @Author 杨友朋
     * @Description TODO 后台管理查询词库
     * @Date 2023/10/12 11:17
     * @Return List<JieLongIdiomQuestion>
     * @param levelId
     */
    @Override
    public List<JieLongIdiomQuestion> selectQuestion(Integer levelId) {
        // 调用持久层接口 查询数据库
        List<JieLongIdiomQuestion> jieLongIdiomQuestions = jieLongIdiomQuestionMapper.selectQuestion(levelId);
        // 返回
        return jieLongIdiomQuestions;
    }

    /**
     * @Author 杨友朋
     * @Description TODO 保存成语信息
     * @Date 2023/10/12 11:22
     * @Return CommonResult
     * @param jieLongIdiomQuestion
     */
    @Override
    public CommonResult saveWordInfo(JieLongIdiomQuestionModel jieLongIdiomQuestion) {
        // 创建实体类对象
        JieLongIdiomQuestion j=new JieLongIdiomQuestion();
        // 模型赋值给实体类
        BeanUtils.copy(jieLongIdiomQuestion, j);
        // 调用持久层接口 保存至数据库
        Integer i=jieLongIdiomQuestionMapper.saveWordInfo(j);
        // 判断是否调用成功
        if(i>0){
            // 成功返回ok
            return CommonResult.ok();
        }
        // 不成功返回error
        return CommonResult.error();
    }

    /**
     * @Author 杨友朋
     * @Description TODO 单查词语信息
     * @Date 2023/10/12 11:20
     * @Return CommonResult
     * @param id
     */
    @Override
    public JieLongIdiomQuestion selectQuestById(Integer id) {
        // 调用持久层接口 查询数据库
        JieLongIdiomQuestion jieLongIdiomQuestion=jieLongIdiomQuestionMapper.selectQuestById(id);
        // 返回
        return jieLongIdiomQuestion;
    }

    /**
     * @Author 杨友朋
     * @Description TODO 添加成语
     * @Date 2023/10/12 11:22
     * @Return CommonResult
     * @param jieLongIdiomLevel
     */
    @Override
    public CommonResult insertIdiom(JieLongIdiomQuestionModel jieLongIdiomLevel) {
        if(jieLongIdiomLevel!=null){
            // 创建实体类对象
            JieLongIdiomQuestion j=new JieLongIdiomQuestion();
            // 创建时间
            jieLongIdiomLevel.setCreateTime(new Date());
            // 修改时间
            jieLongIdiomLevel.setUpdateTime(new Date());
            // 模型赋值给实体类
            BeanUtils.copy(jieLongIdiomLevel, j);
            // 调用持久层接口 保存至数据库
            Integer i=jieLongIdiomQuestionMapper.insertIdiom(j);
            // 判断是否操作成功
            if(i>0){
                // 成功 返回ok
                return CommonResult.ok("添加成功");
            }
            // 失败 返回error
            return CommonResult.error("添加失败");
        }
        return CommonResult.error("参数校验失败");
    }

    /**
     * @Author 杨友朋
     * @Description TODO 删除成语
     * @Date 2023/10/20 13:56
     * @Return CommonResult
     * @param id
     */
    @Override
    public CommonResult delIdiom(Integer id) {
        if(id!=null||id!=0){
            jieLongIdiomQuestionMapper.del(id);
            return CommonResult.ok();
        }
        return CommonResult.error("操作失败");
    }
}
